<template>
    <div class="header">
      <div class="header-container">
        <div class="app-name">
          <img src="image/bg2.jpg" alt="" class="logo">
          <router-link to="/"><b class="app-title">校园二手物品交易平台</b></router-link>
        </div>
        <div class="search-container">
          <el-input placeholder="搜物品..." v-model="searchValue" @keyup.enter.native="searchIdle">
            <el-button slot="append" icon="el-icon-search" @click="searchIdle"></el-button>
          </el-input>
        </div>
        <el-button class="custom-button" icon="el-icon-plus" @click="toRelease">物品发布</el-button>
        <el-button class="custom-button" icon="el-icon-chat-dot-round" @click="toMessage">消息</el-button>
    
        <router-link v-if="!isLogin" class="user-name-text" to="/login">登录</router-link>
        <el-dropdown trigger="click" v-else>
          <div class="user-info">
            <div class="username">{{nicknameValue?nicknameValue:nickname}}</div>
            <el-avatar :src="avatarValue?avatarValue:avatar"></el-avatar>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item><div @click="toMe">个人中心</div></el-dropdown-item>
            <el-dropdown-item divided style="color: red;"><div @click="loginOut">退出登录</div></el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </template>
<script>

    export default {
        name: 'Header',
        props: ['searchInput','nicknameValue','avatarValue'],
        data() {
            return {
                searchValue: this.searchInput,
                nickname:'登录',
                avatar:'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
                isLogin:false
            };
        },
        created(){
            // console.log("header");
            if(! this.$globalData.userInfo.nickname){
                this.$api.getUserInfo().then(res=>{
                    console.log('Header getUserInfo:',res);
                    if(res.status_code===1){
                        this.nickname=res.data.nickname;
                        this.avatar=res.data.avatar;
                        res.data.signInTime=res.data.signInTime.substring(0,10);
                        this.$globalData.userInfo=res.data;
                        this.isLogin=true;
                    }
                })
            }else {
                this.nickname=this.$globalData.userInfo.nickname;
                this.avatar=this.$globalData.userInfo.avatar;
                this.isLogin=true;
            }
        },
        methods: {
            searchIdle() {
                if ('/search' !== this.$route.path) {
                    this.$router.push({path: '/search', query: {searchValue: this.searchValue}});
                } else {
                    this.$router.replace({path: '/search', query: {searchValue: this.searchValue}});
                    this.$router.go(0);
                }

            },
            toMe() {
                if ('/me' !== this.$route.path) {
                    this.$router.push({path: '/me'});
                }
            },
            toMessage(){
                if ('/message' !== this.$route.path) {
                    this.$router.push({path: '/message'});
                }
            },
            toRelease(){
                if ('/release' !== this.$route.path) {
                    this.$router.push({path: '/release'});
                }
            },
            loginOut(){
                this.$api.logout().then(res=>{
                    if(res.status_code===1){
                        this.$globalData.userInfo={};
                        console.log("login out");
                        if ('/index' === this.$route.path) {
                            this.$router.go(0);
                        }else {
                            this.$router.push({path: '/index'});
                        }
                    }else {
                        this.$message.error('网络或系统异常，退出登录失败！');
                    }
                });

            }
        }
    };
</script>

<style scoped>
.custom-button {
  background-color: #ff7f50; /* 这里可以调整为你喜欢的颜色 */
  color: white;
  border: none;
  padding: 10px 20px;
  font-size: 16px;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.custom-button:hover {
  background-color: #ff4500; /* 悬停时的颜色 */
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 60px;
  background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
  display: flex;
  justify-content: center;
  border-bottom: 2px solid #eeeeee;
  z-index: 1000;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.header-container {
  width: 1000px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.app-name {
  display: flex;
  align-items: center;
}

.logo {
  width: 30px;
  position: relative;
  top: 5px;
  margin-right: 10px;
}

.app-title {
  color: #fff;
  font-size: 24px;
  text-decoration: none;
  transition: color 0.3s;
}

.app-title:hover {
  color: #fdbb2d;
}

.search-container {
  width: 300px;
}

.el-input {
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.el-button {
  border-radius: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  font-size: 16px;
  color: #fff;
  padding-right: 10px;
}

.user-name-text {
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  cursor: pointer;
  text-decoration: none;
  transition: color 0.3s;
}

.user-name-text:hover {
  color: #fdbb2d;
}
</style>


<!-- <style scoped> 

    /* .header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        width: 100%;
        height: 58px;
        background: #ffffff;
        display: flex;
        justify-content: center;
        border-bottom: #eeeeee solid 2px;
        z-index: 1000;
    }

    .header-container {
        width: 1000px;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .app-name a {
        color: #409EFF;
        font-size: 24px;
        text-decoration: none;
    }

    .search-container {
        width: 300px;
    }
    .user-name-text{
        font-size: 16px;
        font-weight: 600;
        color: #409EFF;
        cursor: pointer;
        text-decoration: none;
    } 
</style>
*/